package com.zb.framework.config;

import com.google.common.collect.Maps;
import com.zb.framework.service.impl.ActiveDomainServiceImpl;
import lombok.Data;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;

import javax.naming.Context;
import java.security.Security;
import java.util.HashMap;

/**
 * @author fujianjian
 * @date 2023/5/23 10:37
 **/
@Data
@Configuration
@ConfigurationProperties(prefix = "zeus.ldap")
@ConditionalOnProperty(prefix = "zeus.ldap", value = "enable", havingValue = "true")
public class ActiveDirectoryConfig implements InitializingBean {

    private String ldapUrl;
    private String ldapsUrl;
    private String username;
    private String password;
    private String base;

    @Bean
    public LdapContextSource ldapContextSource() {
        LdapContextSource ldapContext = new LdapContextSource();

        HashMap<String, Object> config = Maps.newHashMapWithExpectedSize(1);

        ldapContext.setBase(base);
        ldapContext.setUrl(ldapUrl);
        ldapContext.setUserDn(username);
        ldapContext.setPassword(password);

        //  解决乱码
        config.put("java.naming.ldap.attributes.binary", "objectGUID");
        config.put("com.sun.jndi.ldap.connect.timeout", "50000");
        config.put("com.sun.jndi.ldap.read.timeout", "50000");
        ldapContext.setPooled(false);

        ldapContext.setBaseEnvironmentProperties(config);
        ldapContext.afterPropertiesSet();
        return ldapContext;
    }


    @Bean
    public LdapTemplate zeusLdapTemplate() {
        LdapTemplate ldapTemplate = new LdapTemplate(ldapContextSource());

        ldapTemplate.setIgnorePartialResultException(true);
        ldapTemplate.setIgnoreNameNotFoundException(true);
        return ldapTemplate;
    }

    @Bean
    public LdapContextSource ldapsContextSource() {
        Security.setProperty("jdk.tls.disabledAlgorithms", "");
        System.setProperty("com.sun.jndi.ldap.object.disableEndpointIdentification", "true");
        System.setProperty("javax.net.ssl.trustStore", "C:\\Program Files\\Java\\jdk1.8.0_291\\jre\\lib\\security\\cacerts");

        LdapContextSource ldapContext = new LdapContextSource();

        HashMap<String, Object> config = Maps.newHashMapWithExpectedSize(1);

        ldapContext.setBase(base);
        ldapContext.setUrl(ldapsUrl);
        ldapContext.setUserDn(username);
        ldapContext.setPassword(password);

        //  解决乱码
        config.put("java.naming.ldap.attributes.binary", "objectGUID");
        // config.put("com.sun.jndi.ldap.connect.timeout", "50000");
        // config.put("com.sun.jndi.ldap.read.timeout", "50000");
        config.put("java.naming.ldap.factory.socket", "com.zb.framework.ldap.DummySSLSocketFactory");
        config.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        config.put(Context.SECURITY_PROTOCOL, "ssl");
        config.put(Context.AUTHORITATIVE, "true");
        ldapContext.setPooled(false);

        ldapContext.setBaseEnvironmentProperties(config);
        ldapContext.afterPropertiesSet();
        return ldapContext;
    }


    @Bean
    public LdapTemplate zeusLdapsTemplate() {
        LdapTemplate ldapTemplate = new LdapTemplate(ldapsContextSource());

        ldapTemplate.setIgnorePartialResultException(true);
        ldapTemplate.setIgnoreNameNotFoundException(true);
        return ldapTemplate;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        ActiveDomainServiceImpl.init(base, "%s@jujingsupply.com");
    }
}
